package onlinebanking.client.gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import onlinebanking.client.SimpleClient;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class InitiateTransactionPanel extends javax.swing.JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel initiateTransactionHeader;
	private JButton SubmitButton;
	private JPanel FundsTransferPanel;
	private JComboBox MonthCombo;
	private JComboBox YearCombo;
	private JComboBox DayCombo;
	private JLabel ExpiryDateLabel;
	private JTextField AmmountBalanceField;
	private JLabel AmmountBalanceLabel;
	private JLabel DestinationAccountLabel;
	private JTextField DestAccountTextField;
	private JTabbedPane jTabbedPane1;
	
	private JLabel PACLabel, ConfirmPACLabel;
	private JPasswordField PACField, ConfirmPACField;

	private ClientPanel parent;
	private GregorianCalendar gCalendar;
	private int gcalMonthValues[] = new int[12];
	
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new InitiateTransactionPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public InitiateTransactionPanel() {
		super();		
		initGUI();
	}
	
	public InitiateTransactionPanel(ClientPanel clientPanel) {
		super();
		this.parent = clientPanel;
		initGUI();
		
		
	}
	
	private void initGUI() {
		try {
			{
				this.setLayout(null);
				this.setBounds(0, 0, 521, 444);
				this.setPreferredSize(new java.awt.Dimension(526, 400));
				this.setBackground(new java.awt.Color(190,244,174));
				{
					initiateTransactionHeader = new JLabel();
					this.add(initiateTransactionHeader);
					initiateTransactionHeader.setText("INITIATE TRANSACTION");
					initiateTransactionHeader.setBounds(6, 6, 134, 16);
				}
				{
					jTabbedPane1 = new JTabbedPane();
					this.add(jTabbedPane1);
					jTabbedPane1.setBounds(12, 39, 502, 354);
					jTabbedPane1.setBackground(new java.awt.Color(0,128,0));
					{
						FundsTransferPanel = new JPanel();
						jTabbedPane1.addTab("Funds Transfer", null, FundsTransferPanel, null);
						FundsTransferPanel.setLayout(null);
						FundsTransferPanel.setBackground(new java.awt.Color(190,255,174));
						{
							DestAccountTextField = new JTextField();
							FundsTransferPanel.add(DestAccountTextField);
							DestAccountTextField.setText("");
							DestAccountTextField.setBounds(154, 18, 111, 22);
						}
						{
							DestinationAccountLabel = new JLabel();
							FundsTransferPanel.add(DestinationAccountLabel);
							DestinationAccountLabel.setText("Destination Account:");
							DestinationAccountLabel.setBounds(12, 18, 130, 16);
						}
						{
							AmmountBalanceLabel = new JLabel();
							FundsTransferPanel.add(AmmountBalanceLabel);
							AmmountBalanceLabel.setText("Ammount Balance:");
							AmmountBalanceLabel.setBounds(12, 57, 130, 16);
						}
						{
							AmmountBalanceField = new JTextField();
							FundsTransferPanel.add(AmmountBalanceField);
							AmmountBalanceField.setText("0.0");
							AmmountBalanceField.setBounds(154, 54, 62, 22);
						}
						{
							ExpiryDateLabel = new JLabel();
							FundsTransferPanel.add(ExpiryDateLabel);
							ExpiryDateLabel.setText("Valid until:");
							ExpiryDateLabel.setBounds(12, 106, 71, 16);
						}
						{
							
							MonthCombo = new JComboBox();
							FundsTransferPanel.add(MonthCombo);
							MonthCombo.addItem("Select Month");
							MonthCombo.setBounds(95, 103, 115, 22);
							MonthCombo.addItemListener(new ItemListener() {
								public void itemStateChanged(ItemEvent evt) {
									MonthComboItemStateChanged(evt);
								}
							});
							
						}
						{
							
							DayCombo = new JComboBox();
							FundsTransferPanel.add(DayCombo);
							DayCombo.addItem("Select Day");
							DayCombo.setBounds(216, 103, 105, 22);
						}
						{
							YearCombo = new JComboBox();
							FundsTransferPanel.add(YearCombo);
							YearCombo.setBounds(347, 103, 105, 22);
							YearCombo.addItemListener(new ItemListener() {
								public void itemStateChanged(ItemEvent evt) {
									YearComboItemStateChanged(evt);
								}
							});
						}
						{
							SubmitButton = new JButton();
							FundsTransferPanel.add(SubmitButton);
							SubmitButton.setText("Initialize Transaction");
							SubmitButton.setBounds(321, 287, 157, 28);
							SubmitButton.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) {
									SubmitButtonActionPerformed(evt);
								}
							});
						}
						{
							PACLabel = new JLabel();
							FundsTransferPanel.add(PACLabel);
							PACLabel.setText("Personal Authentication Code");
							PACLabel.setBounds(12, 171, 214, 19);
						}
						{
							ConfirmPACLabel = new JLabel();
							FundsTransferPanel.add(ConfirmPACLabel);
							ConfirmPACLabel.setText("Confirm Personal Authentication Code");
							ConfirmPACLabel.setBounds(12, 207, 246, 16);
						}
						{
							PACField = new JPasswordField();
							FundsTransferPanel.add(PACField);
							PACField.setBounds(276, 169, 132, 22);
						}
						{
							ConfirmPACField = new JPasswordField();
							FundsTransferPanel.add(ConfirmPACField);
							ConfirmPACField.setBounds(276, 204, 132, 22);
						}
					}
				}
				setgCalMonthValues();				
				fillYearComboBox(2010, 10); // TODO edit dynamic start year
				fillMonthComboBox();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void fillMonthComboBox(){
		MonthCombo.removeAllItems();
		MonthCombo.addItem("January");
		MonthCombo.addItem("Febuary");
		MonthCombo.addItem("March");
		MonthCombo.addItem("April");
		MonthCombo.addItem("May");
		MonthCombo.addItem("June");
		MonthCombo.addItem("July");
		MonthCombo.addItem("August");
		MonthCombo.addItem("September");
		MonthCombo.addItem("October");
		MonthCombo.addItem("November");
		MonthCombo.addItem("December");
		setDayComboValues();
	}
	
	private void fillDayCombo(int noOfDays){
		DayCombo.removeAllItems();
		for(int i = 1; i<= noOfDays; i++){
			DayCombo.addItem(i);
		}
	}
	
	private void fillYearComboBox(int startYear,int noOfYears){	
		YearCombo.removeAllItems();
		for(int i = startYear; i<= startYear+noOfYears; i++){
			YearCombo.addItem(i);
		}
	}
	
	private void setDayComboValues(){
		int month = MonthCombo.getSelectedIndex();
		if(month == 1){
			boolean isLeapYear =( ((Integer)(YearCombo.getSelectedItem())).intValue() %4 ==0);
			if(isLeapYear){
				fillDayCombo(29);
			}else{
				fillDayCombo(28);
			}
			
		}else if(month == 0 || month == 2 || month == 4 || month == 6  
				|| month == 7 || month == 9 || month == 11  ){
			fillDayCombo(31);
		}else{
			fillDayCombo(30);
		}
	}
	
	private void setgCalMonthValues(){
		gcalMonthValues[0] = Calendar.JANUARY;
		gcalMonthValues[1] = Calendar.FEBRUARY;
		gcalMonthValues[2] = Calendar.MARCH;
		gcalMonthValues[3] = Calendar.APRIL;
		gcalMonthValues[4] = Calendar.MAY;
		gcalMonthValues[5] = Calendar.JUNE;
		gcalMonthValues[6] = Calendar.JULY;
		gcalMonthValues[7] = Calendar.AUGUST;
		gcalMonthValues[8] = Calendar.SEPTEMBER;
		gcalMonthValues[9] = Calendar.OCTOBER;
		gcalMonthValues[10] = Calendar.NOVEMBER;
		gcalMonthValues[11] = Calendar.DECEMBER;		
	}
	
	private void MonthComboItemStateChanged(ItemEvent evt) {
		setDayComboValues();
	}
	
	private void YearComboItemStateChanged(ItemEvent evt) {
		setDayComboValues();
	}
	
	private void SubmitButtonActionPerformed(ActionEvent evt) {
		//TODO add your code for SubmitButton.actionPerformed
		try{
			double ammountTransfer = Double.parseDouble(AmmountBalanceField.getText());
			int destAccountno = Integer.parseInt(DestAccountTextField.getText());
			
			
			int expDay = DayCombo.getSelectedIndex() + 1;	//adjust from from 0 index
			int expMonth = gcalMonthValues[MonthCombo.getSelectedIndex()];
			int expYear = ((Integer)YearCombo.getSelectedItem()).intValue();
			
			String PAC = new String(PACField.getPassword());
			String ConfirmPAC = new String(ConfirmPACField.getPassword());
			
			try{
				long longPAC = Long.parseLong(PAC);
				long longConfirmPAC = Long.parseLong(ConfirmPAC);
				
				if(PAC.equals(ConfirmPAC)){				
					this.parent.getSimpleClient().initFundsTransfer(ammountTransfer, destAccountno+ "", longPAC, longConfirmPAC, expDay, expMonth, expYear);
					this.parent.switchToInitiateTransaction();
				}else{
					JOptionPane.showMessageDialog(null, "Personal Authentication Codes does't match");
				}
			}catch(NumberFormatException r){
				JOptionPane.showMessageDialog(null, "Incorrect Personal Authentication Code format");
			}
		
		}catch(NumberFormatException e){
			JOptionPane.showMessageDialog(null, "Invalid arguments");
		}
		
		
		
	}

}
